//
//  PayManageViewController.m
//  quxiangtao
//
//  Created by xbmios on 2018/7/12.
//  Copyright © 2018年 shanhaijin. All rights reserved.
//

#import "PayManageViewController.h"
#import "SetPayPasswordViewController.h"

@interface PayManageViewController ()

@property (strong, nonatomic) UIImageView *alipayLogoIV;

@property (strong, nonatomic) UILabel *realNameL;
@property (strong, nonatomic) UITextField *realNameTextField;
@property (strong, nonatomic) UIView *line1;

@property (strong, nonatomic) UILabel *alipayCountL;
@property (strong, nonatomic) UITextField *alipayCountTextField;
@property (strong, nonatomic) UIView *line2;

@property (strong, nonatomic) UIImageView *submitIV;
@property (strong, nonatomic) UILabel *submitL;


@end

@implementation PayManageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = WhiteColor;
    self.title = @"支付管理";
    
    if ([CommonUserInfo sharedInstance].is_Bind) {
        // 已绑定支付宝
        // 获取绑定的支付宝的信息
        [self loadAlipayData];
    }
    
    [self.view addSubview:self.alipayLogoIV];
    
    __weak typeof(self) weakSelf = self;
    [self.alipayLogoIV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(weakSelf.view.mas_centerX);
        make.top.mas_equalTo(Scale_W(20));
        make.width.mas_equalTo(jisuan(100));
        make.height.mas_equalTo(jisuan(100));
    }];
    
    if ([CommonUserInfo sharedInstance].is_Bind) {
        // 绑定了支付宝
        [self createHasBindUI];
        
    }else {
        // 未绑定支付宝
        [self createUI];
        
    }
    
}

#pragma mark -获取绑定支付宝后的信息
- (void)loadAlipayData {
    
    NSString *par = [NSString stringWithFormat:@"{\"user_id\" : \"%@\", }", [RSA encryptString:[CommonUserInfo sharedInstance].user_id publicKey:PublicKey]];
    
    [PPNetworkHelper POST:@"user/Bindingcard/Bindingdetail" parameters:@{@"json" : par} success:^(id responseObject) {
        
        NSLog(@"获取绑定的支付宝信息：%@", responseObject);
        
        if ([responseObject[@"code"] integerValue] == 1) {
            
            NSDictionary *dict = responseObject[@"data"];
            self.alipayCountTextField.text = dict[@"cardid"];
            
        }else {
            
            [[KYAlertView sharedInstance] showAlertViewWithMessage:responseObject[@"msg"]];
            
        }
        
    } failure:^(NSError *error) {
        
        if (error) {
            
            [[KYAlertView sharedInstance] showAlertViewWithMessage:Tip];
        }
    }];
    
}

#pragma mark -提交点击
- (void)submitGestureClick:(UIGestureRecognizer *)recognizer {
    
    NSLog(@"提交点击");
    
    if (self.realNameTextField.text.length <= 0) {
        
        [[KYAlertView sharedInstance] showAlertViewWithMessage:@"请输入您的真实姓名"];
        return ;
    }
    
    if (self.alipayCountTextField.text.length <= 0) {
        
        [[KYAlertView sharedInstance] showAlertViewWithMessage:@"请输入支付宝账号"];
        return;
    }
    
    NSString *par = [NSString stringWithFormat:@"{\"user_id\" : \"%@\", \"name\" : \"%@\", \"cardid\" : \"%@\"}", [RSA encryptString:[CommonUserInfo sharedInstance].user_id publicKey:PublicKey], [RSA encryptString:self.realNameTextField.text publicKey:PublicKey], [RSA encryptString:self.alipayCountTextField.text publicKey:PublicKey]];

    [PPNetworkHelper POST:@"user/Bindingcard/Binding" parameters:@{@"json" : par} success:^(id responseObject) {
        
        NSLog(@"支付管理，绑定支付宝:%@", responseObject);
        
        if ([responseObject[@"code"] integerValue] == 1) {
            
            [CommonUserInfo sharedInstance].is_Bind = YES;
            [[KYAlertView sharedInstance] showAlertViewWithMessage:responseObject[@"msg"]];
            
            if ([CommonUserInfo sharedInstance].is_setPayPassword) {
                
                // 已设置支付密码
                WithDrawViewController *vc = [[WithDrawViewController alloc] init];
                [self.navigationController pushViewController:vc animated:YES];
                
            }else {
                
                // 未设置支付密码
                SetPayPasswordViewController *vc = [[SetPayPasswordViewController alloc] init];
                vc.controllerType = WithdrawType;
                [self.navigationController pushViewController:vc animated:YES];
                
            }
            
//            [self.navigationController popViewControllerAnimated:YES];
            
        }else {
            
            [[KYAlertView sharedInstance] showAlertViewWithMessage:responseObject[@"msg"]];
        }
        
    } failure:^(NSError *error) {
        
        if (error) {
            
            [[KYAlertView sharedInstance] showAlertViewWithMessage:Tip];
            
        }
        
    }];
    
}

- (void)createHasBindUI {
    
    self.alipayCountTextField.enabled = NO;
    
    [self.view addSubview:self.alipayCountL];
    [self.view addSubview:self.alipayCountTextField];
    [self.view addSubview:self.line2];
    
    __weak typeof(self) weakSelf = self;
    [self.alipayCountL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(weakSelf.view.mas_left).with.offset(jisuan(40));
        make.top.mas_equalTo(weakSelf.alipayLogoIV.mas_bottom).with.offset(jisuan(60));
        make.width.mas_equalTo(jisuan(126));
        make.height.mas_equalTo(jisuan(34));
        
    }];
    
    [self.alipayCountTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(weakSelf.alipayCountL.mas_right).with.offset(jisuan(20));
        make.centerY.mas_equalTo(weakSelf.alipayCountL.mas_centerY);
        make.right.mas_equalTo(weakSelf.view.mas_right).with.offset(-jisuan(40));
        make.height.mas_equalTo(jisuan(34));
    }];
    [self.line2 mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(weakSelf.view.mas_left).with.offset(jisuan(40));
        make.top.mas_equalTo(weakSelf.alipayCountL.mas_bottom).with.offset(jisuan(30));
        make.right.mas_equalTo(weakSelf.view.mas_right).with.offset(-jisuan(40));
        make.height.mas_equalTo(jisuan(2));
    }];
    
}

- (void)createUI {
    
    [self.view addSubview:self.realNameL];
    [self.view addSubview:self.realNameTextField];
    [self.view addSubview:self.line1];
    
    [self.view addSubview:self.alipayCountL];
    [self.view addSubview:self.alipayCountTextField];
    [self.view addSubview:self.line2];
    
    [self.view addSubview:self.submitIV];
    [self.view addSubview:self.submitL];
    
    __weak typeof(self) weakSelf = self;
//    [self.alipayLogoIV mas_makeConstraints:^(MASConstraintMaker *make) {
//
//        make.centerX.mas_equalTo(weakSelf.view.mas_centerX);
//        make.top.mas_equalTo(weakSelf.view.mas_top).with.offset(jisuan(38));
//        make.width.mas_equalTo(jisuan(100));
//        make.height.mas_equalTo(jisuan(100));
//    }];
    
    [self.realNameL mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(weakSelf.view.mas_left).with.offset(jisuan(40));
        make.top.mas_equalTo(weakSelf.alipayLogoIV.mas_bottom).with.offset(jisuan(60));
        make.width.mas_equalTo(jisuan(100));
        make.height.mas_equalTo(jisuan(34));
        
    }];
    [self.realNameTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(weakSelf.realNameL.mas_right).with.offset(jisuan(44));
        make.centerY.mas_equalTo(weakSelf.realNameL.mas_centerY);
        make.right.mas_equalTo(weakSelf.view.mas_right).with.offset(-jisuan(40));
        make.height.mas_equalTo(jisuan(34));
    }];
    [self.line1 mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(weakSelf.view.mas_left).with.offset(jisuan(40));
        make.top.mas_equalTo(weakSelf.realNameL.mas_bottom).with.offset(jisuan(30));
        make.right.mas_equalTo(weakSelf.view.mas_right).with.offset(-jisuan(40));
        make.height.mas_equalTo(jisuan(2));
    }];
    
    
    
    [self.alipayCountL mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(weakSelf.view.mas_left).with.offset(jisuan(40));
        make.top.mas_equalTo(weakSelf.line1.mas_bottom).with.offset(jisuan(28));
        make.width.mas_equalTo(jisuan(126));
        make.height.mas_equalTo(jisuan(34));
        
    }];
    [self.alipayCountTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(weakSelf.alipayCountL.mas_right).with.offset(jisuan(20));
        make.centerY.mas_equalTo(weakSelf.alipayCountL.mas_centerY);
        make.right.mas_equalTo(weakSelf.view.mas_right).with.offset(-jisuan(40));
        make.height.mas_equalTo(jisuan(34));
    }];
    [self.line2 mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(weakSelf.view.mas_left).with.offset(jisuan(40));
        make.top.mas_equalTo(weakSelf.alipayCountL.mas_bottom).with.offset(jisuan(30));
        make.right.mas_equalTo(weakSelf.view.mas_right).with.offset(-jisuan(40));
        make.height.mas_equalTo(jisuan(2));
    }];
    
    [self.submitIV mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.mas_equalTo(weakSelf.view.mas_centerX);
        make.bottom.mas_equalTo(weakSelf.view.mas_bottom).with.offset(-jisuan(66));
        make.width.mas_equalTo(jisuan(550));
        make.height.mas_equalTo(jisuan(106));
    }];
    [self.submitL mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.mas_equalTo(weakSelf.submitIV.mas_centerX);
        make.centerY.mas_equalTo(weakSelf.submitIV.mas_centerY).with.offset(-jisuan(4));
    }];
}

#pragma mark -lazy
- (UIImageView *)alipayLogoIV {
    
    if (!_alipayLogoIV) {
        
        _alipayLogoIV = [[UIImageView alloc] init];
        _alipayLogoIV.contentMode = UIViewContentModeScaleAspectFit;
        _alipayLogoIV.image = [UIImage imageNamed:@"alipayBG"];
        
    }
    
    return _alipayLogoIV;
}

- (UILabel *)realNameL {
    
    if (!_realNameL) {
        
        _realNameL = [[UILabel alloc] init];
        _realNameL.text = @"真实姓名";
        _realNameL.textColor = Color333333;
        _realNameL.textAlignment = NSTextAlignmentLeft;
        _realNameL.font = Font(24);
    }
    
    return _realNameL;
}

- (UITextField *)realNameTextField {
    
    if (!_realNameTextField) {
        
        _realNameTextField = [[UITextField alloc] init];
        _realNameTextField.textAlignment = NSTextAlignmentLeft;
        _realNameTextField.textColor = Color333333;
        _realNameTextField.font = Font(24);
        _realNameTextField.placeholder = @"请输入您的真实姓名";
        
        _realNameTextField.keyboardType = UIKeyboardTypeDefault;
        
    }
    
    return _realNameTextField;
}

- (UIView *)line1 {
    
    if (!_line1) {
        
        _line1 = [[UIView alloc] init];
        _line1.backgroundColor = Colordddddd;
    }
    
    return _line1;
}

- (UILabel *)alipayCountL {
    
    if (!_alipayCountL) {
        
        _alipayCountL = [[UILabel alloc] init];
        _alipayCountL.text = @"支付宝账号";
        _alipayCountL.textColor = Color333333;
        _alipayCountL.textAlignment = NSTextAlignmentLeft;
        _alipayCountL.font = Font(24);
    }
    
    return _alipayCountL;
}

- (UITextField *)alipayCountTextField {
    
    if (!_alipayCountTextField) {
        
        _alipayCountTextField = [[UITextField alloc] init];
        _alipayCountTextField.textAlignment = NSTextAlignmentLeft;
        _alipayCountTextField.textColor = Color333333;
        _alipayCountTextField.font = Font(24);
        _alipayCountTextField.placeholder = @"请输入支付宝(邮箱)账号";
        
        _alipayCountTextField.keyboardType = UIKeyboardTypeDefault;
        
    }
    
    return _alipayCountTextField;
}

- (UIView *)line2 {
    
    if (!_line2) {
        
        _line2 = [[UIView alloc] init];
        _line2.backgroundColor = Colordddddd;
    }
    
    return _line2;
}

- (UIImageView *)submitIV {
    
    if (!_submitIV) {
     
        _submitIV = [[UIImageView alloc] init];
        _submitIV.contentMode = UIViewContentModeScaleAspectFit;
        _submitIV.image = [UIImage imageNamed:@"redBtnBG"];
        
        _submitIV.userInteractionEnabled = YES;
        
        UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(submitGestureClick:)];
        [_submitIV addGestureRecognizer:gesture];
    }
    
    return _submitIV;
}


- (UILabel *)submitL {
    
    if (!_submitL) {
        
        _submitL = [[UILabel alloc] init];
        _submitL.text = @"提交";
        _submitL.textColor = WhiteColor;
        _submitL.textAlignment = NSTextAlignmentCenter;
        _submitL.font = Font(32);
    }
    
    return _submitL;
}

#pragma mark -设置
- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = NO;
//    self.tabBarController.tabBar.hidden = YES;
}
- (void)viewWillDisappear:(BOOL)animated {
    
    [super viewWillDisappear:animated];
    
//    self.navigationController.navigationBar.hidden = NO;
//    self.tabBarController.tabBar.hidden = NO;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
